﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.GameInput;
using Terraria.UI;

namespace Terraria.ModLoader.UI
{
	// Token: 0x02000286 RID: 646
	internal class UIInputTextField : UIElement
	{
		// Token: 0x14000033 RID: 51
		// (add) Token: 0x06001A4F RID: 6735 RVA: 0x00428B84 File Offset: 0x00426D84
		// (remove) Token: 0x06001A50 RID: 6736 RVA: 0x00428BBC File Offset: 0x00426DBC
		public event UIInputTextField.EventHandler OnTextChange;

		// Token: 0x06001A51 RID: 6737 RVA: 0x00012C7E File Offset: 0x00010E7E
		public UIInputTextField(string hintText)
		{
			this.hintText = hintText;
		}

		// Token: 0x06001A52 RID: 6738 RVA: 0x00012C98 File Offset: 0x00010E98
		public void SetText(string text)
		{
			if (this.currentString != text)
			{
				this.currentString = text;
				UIInputTextField.EventHandler onTextChange = this.OnTextChange;
				if (onTextChange == null)
				{
					return;
				}
				onTextChange(this, new EventArgs());
			}
		}

		// Token: 0x06001A53 RID: 6739 RVA: 0x00428BF4 File Offset: 0x00426DF4
		protected override void DrawSelf(SpriteBatch spriteBatch)
		{
			PlayerInput.WritingText = true;
			Main.instance.HandleIME();
			string inputText = Main.GetInputText(this.currentString);
			if (!inputText.Equals(this.currentString))
			{
				this.currentString = inputText;
				this.OnTextChange(this, new EventArgs());
			}
			else
			{
				this.currentString = inputText;
			}
			int num = this.textBlinkerCount + 1;
			this.textBlinkerCount = num;
			if (num >= 20)
			{
				this.textBlinkerState = (this.textBlinkerState + 1) % 2;
				this.textBlinkerCount = 0;
			}
			string text = this.currentString;
			if (this.textBlinkerState == 1)
			{
				text += "|";
			}
			CalculatedStyle dimensions = base.GetDimensions();
			if (this.currentString.Length == 0)
			{
				Utils.DrawBorderString(spriteBatch, this.hintText, new Vector2(dimensions.X, dimensions.Y), Color.Gray, 1f, 0f, 0f, -1);
				return;
			}
			Utils.DrawBorderString(spriteBatch, text, new Vector2(dimensions.X, dimensions.Y), Color.White, 1f, 0f, 0f, -1);
		}

		// Token: 0x04001789 RID: 6025
		private string hintText;

		// Token: 0x0400178A RID: 6026
		internal string currentString = "";

		// Token: 0x0400178B RID: 6027
		private int textBlinkerCount;

		// Token: 0x0400178C RID: 6028
		private int textBlinkerState;

		// Token: 0x02000287 RID: 647
		// (Invoke) Token: 0x06001A55 RID: 6741
		public delegate void EventHandler(object sender, EventArgs e);
	}
}
